<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Searching</title>
		<script type="text/javascript" src="static/js/jquery-1.9.1.min.js"></script>
		
		<c:choose>
		    <c:when test="${comboboxSubjectSelection != null}">
			    <script>
			        window.onload = function() 
			        {
			        	//jstl parameetrit saab ka '' vahele sisestada!
			        	displaySelectedSubjectSearchDiv('${comboboxSubjectSelection}');
			        }
		        </script>
	        </c:when>
	        <c:otherwise>
	        	<script>
			        window.onload = function() 
			        {
			        	displaySelectedSubjectSearchDiv('displayDefaultSelection');
			        }
		        </script>
      		</c:otherwise> 
		</c:choose>
		
		<script type="text/javascript">
			
			//displayDefaultSelection paneb tööle kasutaja comboboxis tehtud muudatused ja combobox 
			//defauldi, mis, kui leht uuesti laetakse, aga servletis midagi seatud pole
			function displaySelectedSubjectSearchDiv(comboboxSelectionMade)
			{
				var comboboxSelectionToDisplay = comboboxSelectionMade;
				
				if (comboboxSelectionToDisplay == "displayDefaultSelection")
				{
					comboboxSelectionToDisplay = document.getElementById('comboboxSubjectSelection').value;
					//Comboboxi valiku nimi on vastava div-i identifikaator
					document.getElementById(comboboxSelectionToDisplay).style.display = "inline";	
					hideOthersSubjectSearchDivs(comboboxSelectionToDisplay);
				}
				else //servlet on parameetriks asetanud vormi sisaldavad divi id, mida peaks näitama
				{	
					//Muudame ka combocoxi valiku nime vastavaks väärtusega
					setComboboxTextAsDelegated(comboboxSelectionToDisplay);
					
					document.getElementById(comboboxSelectionToDisplay).style.display = "inline";
					hideOthersSubjectSearchDivs(comboboxSelectionToDisplay);
				}
				
			}
			
			function hideOthersSubjectSearchDivs(comboboxValueThatHasBeenSelectedAndIsDisplayed)
			{
				var comboboxSubjectSelection = document.getElementById('comboboxSubjectSelection');
				for (var i = 0, n = comboboxSubjectSelection.options.length; i < n; i++)
				{
					var divSelected = comboboxSubjectSelection.options[i].value;
					if (divSelected != comboboxValueThatHasBeenSelectedAndIsDisplayed)
					{
						document.getElementById(divSelected).style.display = "none";
					}
				}
			}
			
			function setComboboxTextAsDelegated(comboboxSelectionToDisplay)
			{
				var comboboxObject = document.getElementById('comboboxSubjectSelection');
				for (var i = 0, n = comboboxObject.options.length; i < n; i++)
				{
					var divSelected = comboboxSubjectSelection.options[i].value;
					if (divSelected == comboboxSelectionToDisplay)
					{
						//sean vastava tekstilise väärtuse comboboxile (vastava indeksi)
						comboboxObject.selectedIndex = i;
					}
				}
			}
			
		</script>
		<style type="text/css">
			#personSearchFormContainerDiv 		{display: none}
			#enterpriseSearchFormContainerDiv 	{display: none}
			#employeeSearchFormContainerDiv 	{display: none}
			
			table.subjectResultTable
			{
				font-family: verdana,arial,sans-serif;
				font-size:11px;
				color:#333333;
				border-width: 1px;
				border-color: #666666;
				border-collapse: collapse;
			}
			
			table.subjectResultTable th
			{
				border-width: 1px;
				padding: 8px;
				border-style: solid;
				border-color: #666666;
				background-color: #dedede;
			}
			
			
			
			table.subjectResultTable td
			{
				border-width: 1px;
				padding: 8px;
				border-style: solid;
				border-color: #666666;
				background-color: #ffffff;
			}
			
			p.noSubjectsFound
			{
				font-weight: bold;
			}
			
		</style>
	</head>
	<body>
	<%@ include file="/mainMenu.jsp" %>
    <h1>Otsing</h1>
    
		<form name="subjectSelectionForm">
			<p><b>Soovi korral vahetage subjekti tüüpi, keda otsida soovite.</b></p>
			<select id="comboboxSubjectSelection" onchange="displaySelectedSubjectSearchDiv('displayDefaultSelection')">
			  <option value="personSearchFormContainerDiv">		Isik</option>
			  <option value="enterpriseSearchFormContainerDiv">	Ettevõte</option>
			  <option value="employeeSearchFormContainerDiv">	Töötaja</option>
			  <option value="personTypedCustomerSearchFormContainerDiv">Eraisikust klient</option>
			  <option value="enterpriseTypedCustomerSearchFormContainerDiv">Juriidilisest isikust klient</option>
			</select>
		</form>
		<br/>
		
		<!-- Vaatan, milline combobox on valitud, selle põhjal includen 
		otsingusse otsinguvormi -->
		<p>
			NB! kuupäevasid küsiva väljade puhul, palun sisestada väljaväärtus formaadis yyyy-MM-dd
		</p>
		
		<div id="personSearchFormContainerDiv">
					<%@ include file="/personSearchForm.jsp" %>
					<!-- Võtame vastavad atribuudid, kui nad seatud on-->
					<c:if test="${listOfStringAndSubjectAttributeTypeValuesForPerson != null && not empty listOfStringAndSubjectAttributeTypeValuesForPerson}">
						<c:forEach var="frontendStringAndSubjectAttributeTypeHolder" items="${listOfStringAndSubjectAttributeTypeValuesForPerson}">
							<tr><td><c:out value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" /></td><td><input type="text" name="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeValueInString}"></td></tr>
						</c:forEach>
					</c:if>
					<tr><td colspan="2"><input type="submit" name="personSearchFormSubmitted" value="Otsi"></td></tr>
				</table>
			</form>	
		</div>
		
		<div id="enterpriseSearchFormContainerDiv">
					<%@ include file="/enterpriseSearchForm.jsp" %>
					<!-- Võtame vastavad atribuudid, kui nad seatud on-->
					<c:if test="${listOfStringAndSubjectAttributeTypeValuesForEnterprise != null && not empty listOfStringAndSubjectAttributeTypeValuesForEnterprise}">
						<c:forEach var="frontendStringAndSubjectAttributeTypeHolder" items="${listOfStringAndSubjectAttributeTypeValuesForEnterprise}">
							<tr><td><c:out value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" /></td><td><input type="text" name="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeValueInString}"></td></tr>
						</c:forEach>
					</c:if>
					<tr><td colspan="2"><input type="submit" name="enterpriseSearchFormSubmitted" value="Otsi"></td></tr>
				</table>
			</form>	
		</div>
		
		<div id="employeeSearchFormContainerDiv">
			<%@ include file="/employeeSearchForm.jsp" %>
			<!-- Võtame vastavad atribuudid, kui nad seatud on-->
					<c:if test="${listOfStringAndSubjectAttributeTypeValuesForEmployee != null && not empty listOfStringAndSubjectAttributeTypeValuesForEmployee}">
						<c:forEach var="frontendStringAndSubjectAttributeTypeHolder" items="${listOfStringAndSubjectAttributeTypeValuesForEmployee}">
							<tr><td><c:out value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" /></td><td><input type="text" name="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeValueInString}"></td></tr>
						</c:forEach>
					</c:if>
					<tr><td colspan="2"><input type="submit" name="employeeSearchFormSubmitted" value="Otsi"></td></tr>
				</table>
			</form>	
		</div>
		
		<div id="personTypedCustomerSearchFormContainerDiv">
			<%@ include file="/personTypedCustomerSearchForm.jsp" %>
			<!-- Võtame vastavad atribuudid, kui nad seatud on-->
					<c:if test="${listOfStringAndSubjectAttributeTypeValuesForPersonTypedCustomer != null && not empty listOfStringAndSubjectAttributeTypeValuesForPersonTypedCustomer}">
						<c:forEach var="frontendStringAndSubjectAttributeTypeHolder" items="${listOfStringAndSubjectAttributeTypeValuesForPersonTypedCustomer}">
							<tr><td><c:out value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" /></td><td><input type="text" name="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeValueInString}"></td></tr>
						</c:forEach>
					</c:if>
					<tr><td colspan="2"><input type="submit" name="personTypedCustomerSearchFormSubmitted" value="Otsi"></td></tr>
				</table>
			</form>	
		</div>	
		
		<div id="enterpriseTypedCustomerSearchFormContainerDiv">
			<%@ include file="/enterpriseTypedCustomerSearchForm.jsp" %>
			<!-- Võtame vastavad atribuudid, kui nad seatud on-->
					<c:if test="${listOfStringAndSubjectAttributeTypeValuesForEnterpriseTypedCustomer != null && not empty listOfStringAndSubjectAttributeTypeValuesForEnterpriseTypedCustomer}">
						<c:forEach var="frontendStringAndSubjectAttributeTypeHolder" items="${listOfStringAndSubjectAttributeTypeValuesForEnterpriseTypedCustomer}">
							<tr><td><c:out value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" /></td><td><input type="text" name="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeTypeName}" value="${frontendStringAndSubjectAttributeTypeHolder.subjectAttributeValueInString}"></td></tr>
						</c:forEach>
					</c:if>
					<tr><td colspan="2"><input type="submit" name="enterpriseTypedCustomerSearchFormSubmitted" value="Otsi"></td></tr>
				</table>
			</form>	
		</div>		
		
		
		<!-- TULEMUSTE OSA -->
		
		<!-- KUI TULEMUS ON PERSON-->
		<c:choose>
		    <c:when test="${listOfPersonsToShowOnJSP != null && not empty listOfPersonsToShowOnJSP}">
				<p><b>ISIKU OTSINGU TULEMUSED</b></p>
				<table class="subjectResultTable">
					<tr>
						<th>PersonId</th><th>First name</th><th>Last Name</th><th>Identity code</th><th>Birth date</th><th>Created by</th><th>Updated by</th><th>Created</th><th>Updated</th>
					</tr>
					<c:forEach var="personWhoMatchedWithSearchCriterias" items="${listOfPersonsToShowOnJSP}">
						<tr>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.person}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.first_name}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.last_name}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.identity_code}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.birth_date}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.created_by}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.updated_by}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.created}" /></td>
							<td><c:out value="${personWhoMatchedWithSearchCriterias.updated}" /></td>
						</tr>			
					</c:forEach>
				</table>
			</c:when>
	        <c:otherwise>
	        	<!-- Seda kirja näitame vaid siis, kui otsingusse submititi personSearchForm. Servletil pannakse siia kaasa aga 
	        	vastav comboboxSelection, mis sisaldav div-i, mida peaks näitama. Seega võtame selle aluseks -->
	        	<c:if test="${not empty comboboxSubjectSelection && comboboxSubjectSelection != null && comboboxSubjectSelection == 'personSearchFormContainerDiv'}">
	        		<p class="noSubjectsFound">Kriteeriumitele vastavat/vastavaid isikut/isikuid ei leitud!</p>
	        	</c:if>
	        </c:otherwise>
		</c:choose>
		
		<!-- KUI TULEMUS ON ENTERPRISE-->
		<c:choose>
		    <c:when test="${listOfEnterprisesToShowOnJSP != null && not empty listOfEnterprisesToShowOnJSP}">
				<p><b>ETTEVÕTTE OTSINGU TULEMUSED</b></p>
				<table class="subjectResultTable">
					<tr>
						<th>EnterpriseId</th><th>Name</th><th>Full name</th><th>Created by</th><th>Updated by</th><th>Created</th><th>Updated</th>
					</tr>
					<c:forEach var="enterpriseWhoMatchedWithSearchCriterias" items="${listOfEnterprisesToShowOnJSP}">
						<tr>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.enterprise}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.name}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.full_name}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.created_by}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.updated_by}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.created}" /></td>
							<td><c:out value="${enterpriseWhoMatchedWithSearchCriterias.updated}" /></td>
						</tr>
					</c:forEach>
				</table>
			</c:when>
	        <c:otherwise>
	        	<!-- Seda kirja näitame vaid siis, kui otsingusse submititi enterpriseSearchForm Servletil pannakse siia kaasa aga 
	        	vastav comboboxSelection, mis sisaldav div-i, mida peaks näitama. Seega võtame selle aluseks -->
	        	<c:if test="${not empty comboboxSubjectSelection && comboboxSubjectSelection != null && comboboxSubjectSelection == 'enterpriseSearchFormContainerDiv'}">
	        		<p class="noSubjectsFound">Kriteeriumitele vastavat/vastavaid ettevõtet/ettevõtteid ei leitud!</p>
	        	</c:if>
	        </c:otherwise>
		</c:choose>
		
		<!-- KUI TULEMUS ON EMPLOYEE-->
		<c:choose>
	    	<c:when test="${listOfEmployeesToShowOnJSP != null && not empty listOfEmployeesToShowOnJSP}">
				<p><b>TÖÖTAJA OTSINGU TULEMUSED</b></p>
				<table class="subjectResultTable">
					<tr>
						<th>EmployeeId</th><th>EnterpriseID</th><th>PersonID</th><th>Is Active</th>
					</tr>
					<c:forEach var="employeeWhoMatchedWithSearchCriterias" items="${listOfEmployeesToShowOnJSP}">
						<tr>
							<td><c:out value="${employeeWhoMatchedWithSearchCriterias.employee}" /></td>
							<td><c:out value="${employeeWhoMatchedWithSearchCriterias.enterprise_fk}" /></td>
							<td><c:out value="${employeeWhoMatchedWithSearchCriterias.person_fk}" /></td>
							<td><c:out value="${employeeWhoMatchedWithSearchCriterias.active}" /></td>
						</tr>
					</c:forEach>
				</table>
			</c:when>
	        <c:otherwise>
	        	<!-- Seda kirja näitame vaid siis, kui otsingusse submititi employeeSearchForm Servletil pannakse siia kaasa aga 
	        	vastav comboboxSelection, mis sisaldav div-i, mida peaks näitama. Seega võtame selle aluseks -->
	        	<c:if test="${not empty comboboxSubjectSelection && comboboxSubjectSelection != null && comboboxSubjectSelection == 'employeeSearchFormContainerDiv'}">
	        		<p class="noSubjectsFound">Kriteeriumitele vastavat/vastavaid töötajat/töötajaid ei leitud!</p>
	        	</c:if>
	        </c:otherwise>
		</c:choose>
		
		<!-- KUI TULEMUS ON CUSTOMER TYPED PERSON-->
		<c:choose>
	    	<c:when test="${listOfPersonTypedCustomerToShowOnJSP != null && not empty listOfPersonTypedCustomerToShowOnJSP}">
				<p><b>ERAISIKUST KLIENDI OTSINGU TULEMUSED</b></p>
				<table class="subjectResultTable">
					<tr>
						<th>CustomerId</th><th>Subject ID</th><th>Subject type ID</th>
					</tr>
					<c:forEach var="customerTypedPersonWhoMatchedWithSearchCriterias" items="${listOfPersonTypedCustomerToShowOnJSP}">
						<tr>
							<td><c:out value="${customerTypedPersonWhoMatchedWithSearchCriterias.customer}" /></td>
							<td><c:out value="${customerTypedPersonWhoMatchedWithSearchCriterias.subject_fk}" /></td>
							<td><c:out value="${customerTypedPersonWhoMatchedWithSearchCriterias.subject_type_fk}" /></td>
						</tr>
					</c:forEach>
				</table>
			</c:when>
	        <c:otherwise>
	        	<!-- Seda kirja näitame vaid siis, kui otsingusse submititi personTypedCustomerSearchForm Servletil pannakse siia kaasa aga 
	        	vastav comboboxSelection, mis sisaldav div-i, mida peaks näitama. Seega võtame selle aluseks -->
	        	<c:if test="${not empty comboboxSubjectSelection && comboboxSubjectSelection != null && comboboxSubjectSelection == 'personTypedCustomerSearchFormContainerDiv'}">
	        		<p class="noSubjectsFound">Kriteeriumitele vastavat/vastavaid eraisikust klienti/kliente ei leitud!</p>
	        	</c:if>
	        </c:otherwise>
		</c:choose>
		
		<!-- KUI TULEMUS ON CUSTOMER TYPED ENTERPRISE-->
		<c:choose>
	    	<c:when test="${listOfEnterpriseTypedCustomerToShowOnJSP != null && not empty listOfEnterpriseTypedCustomerToShowOnJSP}">
				<p><b>JURIIDILISEST ISIKUST KLIENDI OTSINGU TULEMUSED</b></p>
				<table class="subjectResultTable">
					<tr>
						<th>CustomerId</th><th>Subject ID</th><th>Subject type ID</th>
					</tr>
					<c:forEach var="customerTypedEnterpriseWhoMatchedWithSearchCriterias" items="${listOfEnterpriseTypedCustomerToShowOnJSP}">			
						<tr>
							<td><c:out value="${customerTypedEnterpriseWhoMatchedWithSearchCriterias.customer}" /></td>
							<td><c:out value="${customerTypedEnterpriseWhoMatchedWithSearchCriterias.subject_fk}" /></td>
							<td><c:out value="${customerTypedEnterpriseWhoMatchedWithSearchCriterias.subject_type_fk}" /></td>
						</tr>
					</c:forEach>
				</table>
			</c:when>
	        <c:otherwise>
	        	<!-- Seda kirja näitame vaid siis, kui otsingusse submititi enterpriseTypedCustomerSearchForm Servletil pannakse siia kaasa aga 
	        	vastav comboboxSelection, mis sisaldav div-i, mida peaks näitama. Seega võtame selle aluseks -->
	        	<c:if test="${not empty comboboxSubjectSelection && comboboxSubjectSelection != null && comboboxSubjectSelection == 'enterpriseTypedCustomerSearchFormContainerDiv'}">
	        		<p class="noSubjectsFound">Kriteeriumitele vastavat/vastavaid eraisikust klienti/kliente ei leitud!</p>
	        	</c:if>
	        </c:otherwise>
		</c:choose>
	</body>
</html>